Strict
Import mojo
Import agfx

Const GESTURE_GRID_START:Int=0
Const GESTURE_GRID_MOVE:Int=1
Const GESTURE_GRID_END:Int=0


Class TGestureGrid
	
	Field position:TVector2
	Field grid:Int[][]
	Field size:TVector2
	Field cell_size:TVector2
	Field gesture_state:Int = GESTURE_GRID_START
	Field current_pos:TVector2
	Field OnStart:TCallback
	Field OnMove:TCallback
	Field OnEnd:TCallback
	Field max_signed_cells:Int=5
	Field enable:Bool=True

	
	'-----------------------------------------------------------------------------------
	'
	'-----------------------------------------------------------------------------------
	
	Method New(pos_x:Int,pos_y:Int,x:Int,y:Int,pixel_w:Int,pixel_h:Int,msc:Int)
		
		Self.grid = IntArray2D(x,y)
		Self.size = New TVector2(x,y)
		Self.cell_size = New TVector2(pixel_w,pixel_h)
		Self.position = New TVector2(pos_x,pos_y)
		Self.current_pos = New TVector2(0,0)
		Self.max_signed_cells = msc
		
		Self.OnStart = New TCallback()
		Self.OnMove = New TCallback()
		Self.OnEnd = New TCallback()
		
		Self.Reset()
		
	End Method
	
	'-----------------------------------------------------------------------------------
	' Update
	'-----------------------------------------------------------------------------------
	Method Update:Void()
	
		If Self.enable Then
			
		
			Local sc:Int=0
			
			If MOUSE.IsInRect(Self.position,VecMult(Self.size,Self.cell_size)) Then
					
				If MOUSE.Hit() Then
					
					Self.gesture_state = GESTURE_GRID_MOVE
					
					' clear grid
					Self.Reset()
					Self.OnStart.Call()
					
				EndIf
				
				If MOUSE.Down() and Self.gesture_state=GESTURE_GRID_MOVE Then
					
					If Self.GetSignedCellsCount()<Self.max_signed_cells Then
							
					
						Self.current_pos.x = (MOUSE.x-Self.position.x) / Self.cell_size.x	
						Self.current_pos.y = (MOUSE.y-Self.position.y) / Self.cell_size.y
						
						
						if Self.current_pos.x>Self.size.x-1 Then Self.current_pos.x = Self.size.x-1
						if Self.current_pos.y>Self.size.y-1 Then Self.current_pos.y = Self.size.y-1
					
						Self.grid[Self.current_pos.x][Self.current_pos.y]=1
					EndIf
					
					Self.OnMove.Call()
					
				EndIf
				
				If MOUSE.Up() Then
					Self.gesture_state = GESTURE_GRID_END
					Self.OnEnd.Call()
				EndIf
			
			EndIf		
		
		EndIf
		
	End Method
	
	'-----------------------------------------------------------------------------------
	' Draw GRID in DEBUG=True
	'-----------------------------------------------------------------------------------
	Method Draw:Void()
	
		For Local cx:Int=0 to Self.size.x-1
			For Local cy:Int=0 to Self.size.y-1
				if Self.grid[cx][cy] = 0 Then
					'SetColor(128,128,128)
				EndIf
				if Self.grid[cx][cy] = 1 Then
					SetAlpha 0.25	
					SetColor(255,255,255)
					'DrawRect(1+Self.position.x+cx*24,1+Self.position.y+cy*24,Self.cell_size.x-2,Self.cell_size.y-2)
					DrawCircle(Self.position.x+cx*24+12,Self.position.y+cy*24+12,8)
				EndIf
				
				
			Next
		Next
	
		If DEBUG Then
			DrawText("MX:"+Int(Self.current_pos.x)+" MY:"+Int(Self.current_pos.y),MOUSE.x,MOUSE.y-12)
		EndIf
		
		SetAlpha 1
		
	End Method
	
	'-----------------------------------------------------------------------------------
	' RESET
	'-----------------------------------------------------------------------------------
	Method Reset:Void()
		For Local cx:Int=0 to Self.size.x-1
			For Local cy:Int=0 to Self.size.y-1
				Self.grid[cx][cy] = 0
			Next
		Next
	End Method
	
	'-----------------------------------------------------------------------------------
	' Get signed cells count
	'-----------------------------------------------------------------------------------
	Method GetSignedCellsCount:Int()
		Local c:Int = 0
		For Local cx:Int=0 to Self.size.x-1
			For Local cy:Int=0 to Self.size.y-1
				If Self.grid[cx][cy] = 1 Then c+=1
			Next
		Next
		Return c
	End Method
End Class